package com.qf.dao.Impl;

import com.qf.dao.SheetDao;
import com.qf.pojo.Sheet;
import com.qf.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class SheetDaoImpl implements SheetDao {
    private QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<Sheet> findAll() {
        try {
            DBUtils.begin();
            String sql = "select id,sheet_name as sheetName from tb_sheet";
            List<Sheet> sheetList = queryRunner.query(DBUtils.getConnection(),
                    sql,
                    new BeanListHandler<>(Sheet.class));
            DBUtils.commit();
            return sheetList;
        }catch (Exception e){
            e.printStackTrace();
            DBUtils.rollback();
        }
        return null;
    }

    @Override
    public void add(String sheetName) {
        try {
            DBUtils.begin();
            String sql = "insert into tb_sheet(sheet_name) values(?)";
            int sheet = queryRunner.update(DBUtils.getConnection(),
                    sql,
                    sheetName);
            DBUtils.commit();
        }catch (Exception e){
            e.printStackTrace();
            DBUtils.rollback();
        }
    }
}
